﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FHost1.Admin
{
    public partial class Uploads : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int pageNo = 1;
                if (Request.QueryString.AllKeys.Contains("page"))
                {
                    pageNo = Convert.ToInt32(Request.QueryString["page"]);
                }

                GetUploadedFiles("", pageNo);
            }
        }

        private void GetUploadedFiles(string searchText, int pageNo)
        {
            int? totalRecs = null;
            if (string.IsNullOrWhiteSpace(searchText))
            {
                using (FHostDataContext dataContext = new FHostDataContext())
                {
                    GridUsers.DataSource = dataContext.GetAllUploads(pageNo, ref totalRecs);
                    GridUsers.DataBind();
                }

                if (totalRecs.HasValue)
                {
                    int totalPage = (totalRecs.Value / 20) + 1;
                    if (totalPage <= 20)
                    {
                        btnPrev.Visible = false;
                        btnNext.Visible = false;
                    }
                    else
                    {
                        btnPrev.Visible = false;
                        btnNext.Visible = true;
                    }
                }
                else
                {
                    btnPrev.Visible = false;
                    btnNext.Visible = false;
                }
            }
            else
            {
                using (FHostDataContext dataContext = new FHostDataContext())
                {
                    GridUsers.DataSource = dataContext.SearchAnyFiles(searchText);
                    GridUsers.DataBind();
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GetUploadedFiles(txtSearch.Text.Trim(), 0);
        }

        protected void lnkDelete_Click(object sender, EventArgs e)
        {
            int fileId = Convert.ToInt32((sender as LinkButton).Attributes["UID"]);
            using (FHostDataContext dataContext = new FHostDataContext())
            {
                dataContext.DeleteFile(fileId);
            }

            int pageNo = 1;
            if (Request.QueryString.AllKeys.Contains("page"))
            {
                pageNo = Convert.ToInt32(Request.QueryString["page"]);
            }

            GetUploadedFiles("", pageNo);
        }

        protected void btnNext_Click(object sender, ImageClickEventArgs e)
        {
            int pageNo = 1;
            int? totalRecs = null;
            if (Request.QueryString.AllKeys.Contains("page"))
            {
                pageNo = Convert.ToInt32(Request.QueryString["page"]);
            }

            using (FHostDataContext dataContext = new FHostDataContext())
            {
                GridUsers.DataSource = dataContext.GetAllUsers(pageNo, ref totalRecs);
                GridUsers.DataBind();
            }

            int totalPage = (totalRecs.Value / 20) + 1;
            if (totalPage <= 20)
            {
                btnPrev.Visible = false;
                btnNext.Visible = false;
            }
            else
            {
                btnPrev.Visible = false;
                btnNext.Visible = true;
            }
        }

        protected void btnPrev_Click(object sender, ImageClickEventArgs e)
        {
            int pageNo = 1;
            if (Request.QueryString.AllKeys.Contains("page"))
            {
                pageNo = Convert.ToInt32(Request.QueryString["page"]);
            }
        }
    }
}